//导入核心模块 拼接地址
const path = require('path')
//导入插件模块
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require("mini-css-extract-plugin")

module.exports = {
  mode: 'production',
  //入口 相对路径
  entry: './src/main.js',
  //出口 绝对路径
  output: {
    path: path.join(__dirname, './dist'),
    filename: 'bundle.js'
  },
  //插件 自动生成html
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html'
    }),
    new MiniCssExtractPlugin({
      filename: './css/index.css'
    })
  ],
  module: {
    rules: [
      {
        //正则表达式
        test: /\.css$/i,
        //从右到左执行原则
        use: ['style-loader', 'css-loader']
      },
      {
        //正则表达式
        test: /\.less$/i,
        //从右到左执行原则
        use: ['style-loader', 'css-loader', 'less-loader']
      },
      {
        test: /\.(png|jpg|gif|jpeg)$/i,
        type: 'asset'
      },
      // 语法降级
      {
        test: /\.js$/,
        //将 node_modules 下所有的包都排除掉, 以提高性能
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            // 配置语法预设包
            presets: ['@babel/preset-env']
          }
        }
      },
      {
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, "css-loader"],
      }
    ]
  }
}